package controller

import (
	myJwt "apiManageGo/src/core/common/jwt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserController struct {
}

func NewUserController() *UserController {
	return &UserController{}
}

type UserLoginVo struct {
	Username string `json:"username"`
	Password string `json:"password"`
}
type UserLoginResp struct {
	Token        string `json:"token"`
	RefreshToken string `json:"refresh_token"`
}

// 登录
func (u *UserController) PostLogin(c *gin.Context) {
	var userLoginVo UserLoginVo
	if err := c.ShouldBindJSON(&userLoginVo); err != nil {
		c.JSON(400, gin.H{
			"msg": "参数错误",
		})
		return
	}
	// TODO: 登录逻辑

	userClaims := myJwt.UserClaims{
		UserId: userLoginVo.Username,
	}

	token, refresh_token_return, err := userClaims.CreateToken()
	if err != nil {
		myErr := &gin.Error{
			Err:  err,
			Type: gin.ErrorTypePublic,
			Meta: "busError",
		}
		c.Errors = append(c.Errors, myErr)
		return
	}
	c.JSON(http.StatusOK, UserLoginResp{
		Token:        token,
		RefreshToken: refresh_token_return,
	})
}

type UserInfoVo struct {
	UserName  string `json:"username"`
	UserEmail string `json:"email"`
}

func (u *UserController) GetUserInfo(c *gin.Context) {
	c.JSON(http.StatusOK, UserInfoVo{
		UserName:  "张三",
		UserEmail: "zhangsan@163.com",
	})
}
